package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.domain.DuolaCar;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaCarService;
import com.qike.duola.service.DuolaGoodsService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "车辆控制器")
@CrossOrigin
public class CarController {
    private static Logger logger = LoggerFactory.getLogger(ApprovalController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaCarService duolaCarService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加车辆
     *
     * @return
     */
    @PostMapping("/carAdd")
    @ApiOperation("添加车辆")
    @ResponseBody
    public BaseResult carAdd(@RequestBody CarAddDto carAddDto) {
        try {
            duolaCarService.carAdd(carAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑车辆
     *
     * @return
     */
    @PostMapping("/carEdit")
    @ApiOperation("编辑车辆")
    @ResponseBody
    public BaseResult carEdit(@RequestBody CarEditDto carEditDto) {
        try {
            duolaCarService.carEdit(carEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除车辆
     *
     * @return
     */
    @PostMapping("/carDel")
    @ApiOperation("删除车辆")
    @ResponseBody
    public BaseResult carDel(@RequestBody DeptDelDto deptDelDto) {
        try {
            duolaCarService.carDel(deptDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 车辆列表
     *
     * @return
     */
    @GetMapping("/carList")
    @ApiOperation("车辆列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CarListVo.class)
    })
    public BaseResult carList(CarListDto carListDto) {
        try {
            CarListVo vo = duolaCarService.carList(carListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 车辆详情
     *
     * @return
     */
    @ApiOperation("车辆详情")
    @GetMapping("/carInfo")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CarInfoVo.class)
    })
    public BaseResult carInfo(Integer id) {
        try {
            CarInfoVo vo = duolaCarService.carInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 根据车牌号和类型下拉车辆
     *
     * @return
     */
    @ApiOperation("根据车牌号和类型下拉车辆")
    @GetMapping("/carPullByTypeOrNo")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CarInfoVo.class)
    })
    public BaseResult carPullByTypeOrNo(Integer Type,String carNo,Double volume) {
        try {
            List<CarPullByTypeOrNo> vo = duolaCarService.carPullByTypeOrNo(Type,carNo,volume);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 所有车辆返回经纬度和方向
     *
     * @return
     */
    @ApiOperation("所有车辆返回经纬度和方向")
    @GetMapping("/getAllCar")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = CarInfoVo.class)
    })
    public BaseResult getAllCar(Integer id) {
        try {
            List<DuolaCar> vo = duolaCarService.getAllCar(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
